using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
using LogLib;

namespace ComLib.ComFun
{
    public class PrintExcel
    {
        private const string NODE_LAYOUT = "Layout";
        private const string NODE_PRINTER = "Printer";
        private const string NODE_PRINTER_NAME= "Name";

        private const string secondDirectory = "\\XDPrint\\PrintExcel.xml";

        private string _baseDirectory = Paths.Directory + "\\YSL";
        public string YSLDirectory
        {
            get { return _baseDirectory; }
            set { _baseDirectory = value; }
        }
      
        private string _printExcelXml = "";
        public string PrintExcelXml
        {
            get
            {
                _printExcelXml = _baseDirectory + secondDirectory;
                return _printExcelXml; 
            }
        }

        public PrintExcel()
        {
            
        }

        public bool GetPrinter(ref string strPrinterName)
        {           
            XmlDocument mXML = new XmlDocument();
            try
            {
                mXML.Load(PrintExcelXml);
                XmlElement root = mXML[NODE_LAYOUT];
                if (root != null)
                {
                    XmlElement printer = root[NODE_PRINTER];
                    if (printer != null)
                    {
                        XmlElement name = printer[NODE_PRINTER_NAME];
                        if (name != null)
                        {
                            strPrinterName = name.InnerText;
                            return true;
                        }
                    }
                }
                return false;
            }
            catch (Exception ex)
            {
                Log.WriteException(ex.ToString());
                return false;
            }         
        }
     
        public bool SetPrinter(string strPrinterName)
        {
            XmlDocument mXML = new XmlDocument();
            try
            {
                mXML.Load(PrintExcelXml);
                XmlElement root = mXML[NODE_LAYOUT];
                if (root != null)
                {
                    XmlElement printer = root[NODE_PRINTER];
                    if (printer != null)
                    {
                        XmlElement name = printer[NODE_PRINTER_NAME];
                        if (name != null)
                        {
                            name.InnerText = strPrinterName;
                            mXML.Save(PrintExcelXml);
                            return true;
                        }
                    }
                }
                return false;
            }
            catch (Exception ex)
            {
                Log.WriteException(ex.ToString());
                return false;
            }        
        }

    }
}
